
//Note: &str input &&  return Option<&str> for lifetime
fn parse<'a> (gtp: &'a str, _key: &str) -> Option<&'a str>{
    Some(gtp)
}

use std::str;
pub fn main(){
    // return Option<&str>
    let gtp = "X01=hello,X02=world";
    let key = "X02";
    if let Some(v) = parse(gtp, key) {
        println!("value of key is : {}", v);
    }


    //1. from &[u8] to &str
    {
        //1.1 as_bytes
        let bytes: &[u8] = "hello".as_bytes();
        println!("from &[u8] to &str: {}" , std::str::from_utf8(bytes).unwrap());
        //1.2 slice of bytes
        let bytes_array_ref: &[u8] = &bytes[0..1];
        println!("from &[u8] to &str ->subslice of bytes: {}" , str::from_utf8(bytes_array_ref).unwrap());
    }

    //2. from [u8] -> to &str
    {
        let bytes: [u8; 5] = [104, 101, 108, 108, 111];
        println!("from [u8] to &str : {}", str::from_utf8(&bytes).unwrap());
    }

    {
        //from u8 -> to &str
        let b = 101_u8;
        let bytes = [b,];
        println!("from u8 to &str : {}", str::from_utf8(&bytes).unwrap());
    }

//create String
    //from vec<u8> to String
    {
        let v = vec![104, 101, 108, 108, 111]; 
        println!("from vec[u8] to String: {}", String::from_utf8(v).unwrap()); //v is moved
    } 
    //from &[u8] to String (vec<u8>)
    {
        let bytes = "hello".as_bytes();
        let v = Vec::from(bytes);// copy from &[u8] to vec 
        println!("from &[u8] to String: {}", String::from_utf8(v).unwrap()); // v is moved
    }
    //from u8 to String
    {
        //create vec from u8
        let v = vec![104, 101, 108, 108, 111]; //moved ownership
        println!("from u8 to String: {}", String::from_utf8(v).unwrap()); // v is moved

    }
}
